package day09.test2;

class Ticket2 {
    static int num = 10000;

    public static void Sell_Ticket() {
        synchronized (Ticket2.class) {
            if (num > 0) {
                num--;
                System.out.println("现在是" + Thread.currentThread().getName() + "正在售票,还剩" + num + "张");
            }
        }
    }
}

public class test_synchronized_static {
    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            while (true) {
                Ticket2.Sell_Ticket();
                if (Ticket2.num <= 0) {
                    break;
                }
            }
        }, "售票线程1");

        Thread thread2 = new Thread(() -> {
            while (true) {
                Ticket2.Sell_Ticket();
                if (Ticket2.num <= 0) {
                    break;
                }
            }
        }, "售票线程2");

        Thread thread3 = new Thread(() -> {
            while (true) {
                Ticket2.Sell_Ticket();
                if (Ticket2.num <= 0) {
                    break;
                }
            }
        }, "售票线程3");

        thread1.start();
        thread2.start();
        thread3.start();
    }
}